// common/tabbar/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    currentTab: null, // 默认首页为选中页面
    tabActive: "",
    items: [
      {
        tabbar_name: "首页",
        tabbar_url: "/pages/index/index",
        tabbar_linktype: "page",
        tabbar: "1",
        tabbar_type: "Article",
        tabimginput_1: "/image/home_A.png",
        tabimginput_2: "/image/home.png",
      },
      {
        tabbar_name: "福利",
        tabbar_url: "/pages/present/index",
        tabbar_linktype: "page",
        tabbar: "1",
        tabbar_type: "Article",
        tabimginput_1: "/image/gift_A.png",
        tabimginput_2: "/image/gift.png",
      },


      {
        tabbar_name: "订单",
        tabbar_url: "/pages/order/index",
        tabbar_linktype: "page",
        tabbar: "1",
        tabbar_type: "Article",
        tabimginput_1: "/image/order_A.png",
        tabimginput_2: "/image/order.png",
      },
      {
        tabbar_name: "我的",
        tabbar_url: "/pages/person/index",
        tabbar_linktype: "page",
        tabbar: "1",
        tabbar_type: "Article",
        tabimginput_1: "/image/person_A.png",
        tabimginput_2: "/image/person.png",
      },
    ],
  },
  attached: function () {
    let that = this;

    let CurrentPageUrl = that.getCurrentPageUrl();
    that.data.items.forEach((item, index) => {
      if (item.tabbar_url.split("?")[0] == "/" + CurrentPageUrl) {
        that.setData({
          currentTab: index,
          tabActive: true,
        });
        that.triggerEvent("tabActive", true);
      }
    });
    wx.hideHomeButton();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    swichNav: function (e) {
      let that = this;
      if (this.data.currentTab === e.target.dataset.current) {
        return false;
      } else {
        let { url, index } = e.currentTarget.dataset;
        console.log(url, index);
        that.setData({
          currentTab: index,
        });
        console.log(this.data.currentTab, e);
        wx.reLaunch({
          url: url,
        });
      }
    },
    getCurrentPageUrl() {
      let pages = getCurrentPages(); //获取加载的页面
      let currentPage = pages[pages.length - 1]; //获取当前页面的对象
      let url = currentPage.route; //当前页面url
      // console.log(pages, currentPage, url);
      return url;
    },
  },
});
